/*
 * comm_dllutil.h
 *
 *  Created on: 2010-1-7
 *      Author: gavinkwoe
 */

#ifndef COMM_LLIST_H_
#define COMM_LLIST_H_

#include "comm_features.h"
#include "comm_std.h"
#include "comm_objbase.h"

/*************************************************************************
* PROTOTYPE
*************************************************************************/

struct __struct_comm_llink;
typedef struct __struct_comm_llink			comm_llink_t;
typedef struct __struct_comm_llink *		comm_llink_ptr_t;

struct __struct_comm_llist;
typedef struct __struct_comm_llist			comm_llist_t;
typedef struct __struct_comm_llist *		comm_llist_ptr_t;

/*************************************************************************
* STRUCTURE
*************************************************************************/

struct __struct_comm_llink
{
	comm_llink_ptr_t	next;
	comm_llink_ptr_t	prev;
};

struct __struct_comm_llist
{
	unsigned long		count;
	comm_llink_ptr_t	head;
	comm_llink_ptr_t	tail;
};

/*************************************************************************
* EXPORTED FUNCTION
*************************************************************************/
#ifdef __cplusplus
extern "C" {
#endif

COMM_IMPORT void				comm_ll_push_head( comm_llist_ptr_t list, comm_llink_ptr_t link );
COMM_IMPORT void				comm_ll_push_tail( comm_llist_ptr_t list, comm_llink_ptr_t link );
COMM_IMPORT comm_llink_ptr_t	comm_ll_pop_head( comm_llist_ptr_t list );
COMM_IMPORT comm_llink_ptr_t	comm_ll_pop_tail( comm_llist_ptr_t list );

COMM_IMPORT void				comm_ll_insert( comm_llist_ptr_t list, comm_llink_ptr_t pos, comm_llink_ptr_t link );
COMM_IMPORT void				comm_ll_remove( comm_llist_ptr_t list, comm_llink_ptr_t link );
COMM_IMPORT void				comm_ll_sort( comm_llist_ptr_t list, comm_cmp_fptr_t cmp_func );


#ifdef __cplusplus
}
#endif //extern "C"

/*************************************************************************
* MACRO
*************************************************************************/

#define COMM_LLINK_INIT( __p ) \
		{ \
			(__p)->prev = NULL; \
			(__p)->next = NULL; \
		}

#define COMM_LLIST_INIT( __p ) \
		{ \
			(__p)->count = 0; \
			(__p)->head = NULL; \
			(__p)->tail = NULL; \
		}

#endif /* #ifndef COMM_LLIST_H_ */
